package com.gold.rabbitmqdemo.study2;

import com.gold.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class WorkConsumer1 {

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection= ConnectionUtils.getConnection();

        Channel channel=connection.createChannel();

        String queueName="test_work_queue";

        channel.queueDeclare(queueName,false,false,false,null);

        Consumer consumer=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                super.handleDelivery(consumerTag, envelope, properties, body);

                String message=new String(body,"UTF-8");
                System.out.println("work queue 1 reveviced : "+message);

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    System.out.println("work queue 1 done.");
                }

            }
        };

        //消息确认
        boolean autoAck=true;
        channel.basicConsume(queueName,autoAck,consumer);



    }
}
